<template>
  <div class='dashboard-container'>
    <div class='container'>
      <div class='tableBar'>
        <label style='margin-right: 10px'>套餐名称：</label>
        <el-input v-model='input'
                  clearable
                  placeholder='请填写套餐名称'
                  style='width: 14%'
                  @clear='init'
                  @keyup.enter.native='initFun' />

        <label style='margin-right: 10px; margin-left: 20px'>套餐分类：</label>
        <el-select v-model='categoryId'
                   clearable
                   placeholder='请选择'
                   style='width: 14%'
                   @clear='init'>
          <el-option v-for='item in dishCategoryList'
                     :key='item.value'
                     :label='item.label'
                     :value='item.value' />
        </el-select>

        <label style='margin-right: 10px; margin-left: 20px'>售卖状态：</label>
        <el-select v-model='dishStatus'
                   clearable
                   placeholder='请选择'
                   style='width: 14%'
                   @clear='init'>
          <el-option v-for='item in saleStatus'
                     :key='item.value'
                     :label='item.label'
                     :value='item.value' />
        </el-select>
        <el-button class='normal-btn continue'
                   @click='init(true)'>
          查询
        </el-button>
        <div class='tableLab'>
          <span class='delBut non'
                @click="deleteHandle('批量')">批量删除</span>
          <!-- <span class="blueBug non" @click="statusHandle('1')">批量启售</span>
          <span
            style="border: none"
            class="delBut non"
            @click="statusHandle('0')"
            >批量停售</span
          > -->
          <el-button style='margin-left: 15px'
                     type='primary'
                     @click="addSetMeal('add')">
            + 新建套餐
          </el-button>
        </div>
      </div>
      <el-table v-if='tableData.length'
                :data='tableData'
                class='tableBox'
                stripe
                @selection-change='handleSelectionChange'>
        <el-table-column type='selection'
                         width='25' />
        <el-table-column label='套餐名称'
                         prop='name' />
        <el-table-column label='图片'
                         prop='image'>
          <template slot-scope='{ row }'>
            <el-image :src='row.image'
                      style='width: 80px; height: 40px; border: none; cursor: pointer'>
              <div slot='error'
                   class='image-slot'>
                <img src='./../../assets/noImg.png'
                     style='width: auto; height: 40px; border: none'>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label='套餐分类'
                         prop='categoryName' />
        <el-table-column label='套餐价'
                         prop='price'>
          <template slot-scope='scope'>
            <span>￥{{ ((scope.row.price).toFixed(2) * 100) / 100 }}</span>
          </template>
        </el-table-column>
        <el-table-column label='售卖状态'>
          <template slot-scope='scope'>
            <div :class="{ 'stop-use': String(scope.row.status) === '0' }"
                 class='tableColumn-status'>
              {{ String(scope.row.status) === '0' ? '停售' : '启售' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label='最后操作时间'
                         prop='updateTime'>
          <!-- <template slot-scope="scope">
            {{ moment(scope.row.lastUpdateTime).format('YYYY-MM-DD h:m:s') }}
          </template> -->
        </el-table-column>
        <el-table-column align='center'
                         label='操作'
                         width='250'>
          <template slot-scope='scope'>
            <el-button class='blueBug'
                       size='small'
                       type='text'
                       @click='addSetMeal(scope.row)'>
              修改
            </el-button>
            <el-button class='delBut'
                       size='small'
                       type='text'
                       @click="deleteHandle('单删', scope.row.id)">
              删除
            </el-button>
            <el-button :class="{
                         blueBug: scope.row.status == '0',
                         delBut: scope.row.status != '0'
                       }"
                       class='blueBug non'
                       size='small'
                       type='text'
                       @click='statusHandle(scope.row)'>
              {{ scope.row.status == '0' ? '启售' : '停售' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Empty v-else
             :is-search='isSearch' />
      <el-pagination v-if='counts > 10'
                     :page-size='pageSize'
                     :page-sizes='[10, 20, 30, 40]'
                     :total='counts'
                     class='pageList'
                     layout='total, sizes, prev, pager, next, jumper'
                     @size-change='handleSizeChange'
                     @current-change='handleCurrentChange' />
    </div>
  </div>
</template>

<script lang='ts'>
import { Component, Vue } from 'vue-property-decorator'
import HeadLable from '@/components/HeadLable/index.vue'
import {
  getSetmealPage,
  editSetmeal,
  deleteSetmeal,
  setmealStatusByStatus,
  dishCategoryList
} from '@/api/setMeal'
import InputAutoComplete from '@/components/InputAutoComplete/index.vue'
import Empty from '@/components/Empty/index.vue'
import { baseUrl } from '@/config.json'

@Component({
  name: 'package',
  components: {
    HeadLable,
    InputAutoComplete,
    Empty
  }
})
export default class extends Vue {
  private input: any = ''
  private counts: number = 0
  private page: number = 1
  private pageSize: number = 10
  private checkList: any[] = []
  private tableData: [] = []
  private dishCategoryList = []
  private categoryId = ''
  private dishStatus = ''
  private isSearch: boolean = false
  private saleStatus: any = [
    {
      value: 0,
      label: '停售'
    },
    {
      value: 1,
      label: '启售'
    }
  ]

  created() {
    this.init()
    this.getDishCategoryList()
  }

  initProp(val) {
    this.input = val
    this.initFun()
  }

  initFun() {
    this.page = 1
    this.init()
  }

  private async init(isSearch?) {
    this.isSearch = isSearch
    await getSetmealPage({
      page: this.page,
      pageSize: this.pageSize,
      name: this.input || undefined,
      categoryId: this.categoryId || undefined,
      status: this.dishStatus
    })
      .then(res => {
        if (res && res.data && res.data.code === 1) {
          this.tableData = res.data.data.records
          this.counts = Number(res.data.data.total)
        } else {
          this.$message.error(res.data.msg)
        }
      })
      .catch(err => {
        this.$message.error('请求出错了：' + err.message)
      })
  }

  // 添加更改
  private addSetMeal(st: any) {
    if (st === 'add') {
      this.$router.push({ path: '/setmeal/add' })
    } else {
      this.$router.push({ path: '/setmeal/add', query: { id: st.id } })
    }
  }

  // 删除
  private deleteHandle(type: string, id: any) {
    if (type === '批量' && id === null) {
      if (this.checkList.length === 0) {
        return this.$message.error('请选择删除对象')
      }
    }
    this.$confirm('确定删除该套餐?', '确定删除', {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      deleteSetmeal(type === '批量' ? this.checkList.join(',') : id)
        .then(res => {
          if (res.data.code === 1) {
            this.$message.success('删除成功！')
            this.init()
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch(err => {
          this.$message.error('请求出错了：' + err.message)
        })
    })
  }

  //状态更改
  private statusHandle(row: any) {
    let params: any = {}
    if (typeof row === 'string') {
      if (this.checkList.length == 0) {
        this.$message.error('批量操作，请先勾选操作菜品！')
        return false
      }
      params.ids = this.checkList.join(',')
      params.status = row
    } else {
      params.ids = row.id
      params.status = row.status ? '0' : '1'
    }

    this.$confirm('确认更改该套餐状态?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      setmealStatusByStatus(params)
        .then(res => {
          if (res.data.code === 1) {
            this.$message.success('套餐状态已经更改成功！')
            this.init()
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch(err => {
          this.$message.error('请求出错了：' + err.message)
        })
    })
  }

  //获取套餐分类下拉数据
  private getDishCategoryList() {
    dishCategoryList({
      type: 2
    })
      .then(res => {
        if (res && res.data && res.data.code === 1) {
          this.dishCategoryList = (
            res.data &&
            res.data.data &&
            res.data.data
          ).map(item => {
            return { value: item.id, label: item.name }
          })
        }
      })
      .catch(() => {
      })
  }

  // 全部操作
  private handleSelectionChange(val: any) {
    let checkArr: string[] = []
    val.forEach((n: any) => {
      checkArr.push(n.id)
    })
    this.checkList = checkArr
  }

  private handleSizeChange(val: any) {
    this.pageSize = val
    this.init()
  }

  private handleCurrentChange(val: any) {
    this.page = val
    this.init()
  }
}
</script>
<style lang='scss'>
.el-table-column--selection .cell {
  padding-left: 10px;
}
</style>
<style lang='scss' scoped>
.dashboard {
  &-container {
    margin: 30px;

    .container {
      background: #fff;
      position: relative;
      z-index: 1;
      padding: 30px 28px;
      border-radius: 4px;

      .tableBar {
        margin-bottom: 20px;

        .tableLab {
          float: right;

          span {
            cursor: pointer;
            display: inline-block;
            font-size: 14px;
            padding: 0 20px;
            color: $gray-2;
          }
        }
      }

      .tableBox {
        width: 100%;
        border: 1px solid $gray-5;
        border-bottom: 0;
      }

      .pageList {
        text-align: center;
        margin-top: 30px;
      }

      //查询黑色按钮样式
      .normal-btn {
        background: #333333;
        color: white;
        margin-left: 20px;
      }
    }
  }
}
</style>
